package com.wireless.serialservice;

public class SerialCommand {
	public byte[] mbuffer = new byte[19];
	public byte startCode = (byte) 0xFA;//new byte {(byte) 0xFA};
	public byte[] length = new byte[]{0x00, 0x12};
	public byte[] src = new byte[]{(byte) 0xFE, (byte) 0xFE};
	public byte[] dest = new byte[]{(byte) 0xFF, (byte) 0xFF};
	public byte[] serialnumber = new byte[]{0x00, 0x00};
	
	/*public byte[] buildCommand(String number) {
		String b = null;
		mbuffer[0] = startCode;
		mbuffer[1] = (byte) 0x9B;
		mbuffer[2] = (byte) 0x88;
		mbuffer[3] = length[0];
		mbuffer[4] = length[1];
		mbuffer[5] = src[0];
		mbuffer[6] = src[1];
		mbuffer[7] = dest[0];
		mbuffer[8] = dest[1];
		mbuffer[9] = serialnumber[0];
		mbuffer[10] = serialnumber[1];
		mbuffer[11] = 0x02;
		mbuffer[12] = 0x02;
		mbuffer[13] = 0x00;
		mbuffer[14] = 0x00;
		b = encode(number);
		byte[] buf = b.getBytes();
		mbuffer[15] = buf[1];
		mbuffer[16] = buf[3];
		mbuffer[17] = buf[5];
		mbuffer[18] = buf[7];
		
		printHexString(mbuffer);
		//mbuffer[7] = (byte) (~(mbuffer[3] + mbuffer[4] + mbuffer[5]
          //      + mbuffer[6] + mbuffer[7] + mbuffer[8] + mbuffer[9] + mbuffer[10] + mbuffer[11] + mbuffer[12]
            //            + mbuffer[13] + mbuffer[14] + mbuffer[15] + mbuffer[16] +mbuffer[17] + mbuffer[18]));

		return mbuffer;
	}*/
	
	public byte[] buildCommand(String number) {
		String b = null;
		mbuffer[0] = (byte) 0xFA;
		//mbuffer[1] = (byte) 0x9B;
		//mbuffer[2] = (byte) 0x88;
		mbuffer[3] = 0x00;
		mbuffer[4] = 0x12;
		mbuffer[5] = (byte) 0xFE;
		mbuffer[6] = (byte) 0xFE;
		mbuffer[7] = (byte) 0xFF;
		mbuffer[8] = (byte) 0xFF;
		mbuffer[9] = 0x00;
		mbuffer[10] = 0x00;
		mbuffer[11] = 0x02;
		mbuffer[12] = 0x02;
		mbuffer[13] = 0x00;
		mbuffer[14] = 0x00;
		b = encode(number);
		byte[] buf = b.getBytes();
		mbuffer[15] = buf[1];
		mbuffer[16] = buf[3];
		mbuffer[17] = buf[5];
		mbuffer[18] = buf[7];
				
		short dd=(short) (getShort(mbuffer, 3)+getShort(mbuffer, 5)+getShort(mbuffer, 7) +getShort(mbuffer, 9)+
				getShort(mbuffer, 11)+getShort(mbuffer, 13)+getShort(mbuffer, 15)+getShort(mbuffer, 17));
		short ddd = (short) ~(dd);
		putShort(mbuffer, ddd, 1);
		return mbuffer;
	}
	
	public static void putShort(byte b[], short s, int index) {  
        b[index + 0] = (byte) (s >> 8);  
        b[index + 1] = (byte) (s >> 0);  
    } 
	public static short getShort(byte[] b, int index) {  
        return (short) (((b[index + 0] << 8) | b[index + 1] & 0xff));  
    }
	
	public byte[] buildSerCameraCommand() {
		mbuffer[0] = startCode;
		mbuffer[1] = (byte) 0xFC;
		mbuffer[2] = (byte) 0xE8;
		mbuffer[3] = 0x00;
		mbuffer[4] = 0x12;
		mbuffer[5] = src[0];
		mbuffer[6] = src[1];
		mbuffer[7] = dest[0];
		mbuffer[8] = dest[1];
		mbuffer[9] = serialnumber[0];
		mbuffer[10] = serialnumber[1];
		mbuffer[11] = 0x02;
		mbuffer[12] = 0x06;
		mbuffer[13] = 0x00;
		mbuffer[14] = 0x00;
		mbuffer[15] = 0x01;
		mbuffer[16] = 0x01;
		mbuffer[17] = 0x01;
		mbuffer[18] = 0x01;
		
		printHexString(mbuffer);
		return mbuffer;
	}
	
	public byte[] buildCloseCommand() {
		mbuffer[0] = startCode;
		mbuffer[1] = (byte) 0xFC;
		mbuffer[2] = (byte) 0xEB;
		mbuffer[3] = 0x00;
		mbuffer[4] = 0x12;
		mbuffer[5] = src[0];
		mbuffer[6] = src[1];
		mbuffer[7] = dest[0];
		mbuffer[8] = dest[1];
		mbuffer[9] = serialnumber[0];
		mbuffer[10] = serialnumber[1];
		mbuffer[11] = 0x02;
		mbuffer[12] = 0x03;
		mbuffer[13] = 0x00;
		mbuffer[14] = 0x00;
		mbuffer[15] = 0x01;
		mbuffer[16] = 0x01;
		mbuffer[17] = 0x01;
		mbuffer[18] = 0x01;
		
		printHexString(mbuffer);
		return mbuffer;
	}
	
	public byte[] buildOpenCommand2() {
		mbuffer[0] = startCode;
		mbuffer[1] = (byte) 0xFC;
		mbuffer[2] = (byte) 0xE9;
		mbuffer[3] = 0x00;
		mbuffer[4] = 0x12;
		mbuffer[5] = src[0];
		mbuffer[6] = src[1];
		mbuffer[7] = dest[0];
		mbuffer[8] = dest[1];
		mbuffer[9] = serialnumber[0];
		mbuffer[10] = serialnumber[1];
		mbuffer[11] = 0x02;
		mbuffer[12] = 0x05;
		mbuffer[13] = 0x00;
		mbuffer[14] = 0x00;
		mbuffer[15] = 0x01;
		mbuffer[16] = 0x01;
		mbuffer[17] = 0x01;
		mbuffer[18] = 0x01;
		
		printHexString(mbuffer);
		return mbuffer;
	}
	
	public byte[] buildOpenCommand() {
		mbuffer[0] = startCode;
		mbuffer[1] = (byte) 0xFC;
		mbuffer[2] = (byte) 0xEA;
		mbuffer[3] = 0x00;
		mbuffer[4] = 0x12;
		mbuffer[5] = src[0];
		mbuffer[6] = src[1];
		mbuffer[7] = dest[0];
		mbuffer[8] = dest[1];
		mbuffer[9] = serialnumber[0];
		mbuffer[10] = serialnumber[1];
		mbuffer[11] = 0x02;
		mbuffer[12] = 0x04;
		mbuffer[13] = 0x00;
		mbuffer[14] = 0x00;
		mbuffer[15] = 0x01;
		mbuffer[16] = 0x01;
		mbuffer[17] = 0x01;
		mbuffer[18] = 0x01;
		
		printHexString(mbuffer);
		return mbuffer;
	}
	
	public static void printHexString(byte[] b) { 
		//System.out.print(hint); 
		for (int i = 0; i < b.length; i++) { 
		String hex = Integer.toHexString(b[i] & 0xFF); 
		if (hex.length() == 1) { 
		hex = '0' + hex; 
		} 
		System.out.print(hex.toUpperCase() + " "); 
		} 
		System.out.println(""); 
		} 
	
	 public static String encode2(String str)
     {
        System.out.println("=========encode========:" +str);
         char[] chars = "0123456789ABCDEF".toCharArray();
         StringBuilder sb = new StringBuilder("");
         byte[] bs = str.getBytes();
         int bit;

         for (int i = 0; i < bs.length; i++)
         {
             bit = (bs[i] & 0xf0) >> 4;
             sb.append(chars[bit]);
             bit = bs[i] & 0x0f;
             sb.append(chars[bit]);
         }
         return sb.toString().trim();
     }
	 
	 public static String encode(String s) 
	 { 
	 String str=""; 
	 for (int i=0;i<s.length();i++) 
	 { 
	 int ch = (int)s.charAt(i); 
	 String s4 = Integer.toHexString(ch); 
	 str = str + s4; 
	 } 
	 return str; 
	 } 

	public static String encode1(String str) 
	{ 
	//根据默认编码获取字节数组 
	  String hexString="0123456789ABCDEF"; 
	  byte[] bytes = str.getBytes(); 
	  StringBuilder sb = new StringBuilder(bytes.length*2); 
	  //将字节数组中每个字节拆解成2位16进制整数 
	  for(int i=0; i<bytes.length; i++) 
	  { 
	     sb.append(hexString.charAt((bytes[i]&0xf0)>>4)); 
	     sb.append(hexString.charAt((bytes[i]&0x0f)>>0)); 
	  } 
	  return sb.toString(); 
	}
}
